<?php

namespace App\Console\Commands\CustomerCoupon;

use App\Kuafu\CustomerCoupon\Enum\CodeEnum;
use App\Kuafu\CustomerCoupon\Model\CustomerCouponModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class RefreshCustomerCouponUseInfo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'customerCoupon:refreshUseInfo';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '优惠券写入使用时间';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        CustomerCouponModel::query()
                           ->where("status", CodeEnum::COUPON_STATUS_USED)
                           ->where("useTime", 0)
                           ->chunk(100, function (Collection $customerCouponModelMap) {
                               $customerCouponModelMap->each(function (CustomerCouponModel $customerCouponModel) {
                                   $customerCouponModel->useTime = $customerCouponModel->updatedAt->timestamp;
                                   $customerCouponModel->save();
                               });
                           });

        return parent::SUCCESS;
    }
}
